#include "file_link.h"

//初始化链表
piclink init_link(void)
{
    return NULL;
}

//新建节点
piclink newnode(struct img_info *data)
{
    piclink newnode=calloc(1,sizeof(node));
    if(newnode!=NULL)
    {
        newnode->data=data;
        newnode->next=newnode;
        newnode->prev=newnode;
    }
    return newnode;
}

//添加节点(头插法)
piclink pic_add_link(piclink head,piclink new)
{
    if(head==NULL)
    {
        return new;
    }
    else
    {
        new->prev=head;
        new->next=head->next;

        head->next->prev=new;
        head->next=new;
    }
    return head;
}

//释放节点
piclink tunc_list(piclink head)
{
    if(head == NULL)
        return NULL;
    
    piclink p = head;
    piclink q = NULL;
    do{
        q = p->next;
        free(p);
        p = q;
    }while(p != NULL && q != p);

    return NULL;
}

//对图片进行解码
void load_img(piclink head)
{
    //对图片进行解码
    enum imgfmt {JPG, PNG, BMP};
    enum imgfmt fmt;
    if(strstr(head->data->name, ".jpg") || strstr(head->data->name, "jprg"))
        fmt = JPG;
    if(strstr(head->data->name, ".bmp"))
        fmt = BMP;
    if(strstr(head->data->name, ".png"))
        fmt = PNG;

    switch (fmt)
    {
    case JPG:
        load_jpg(head->data->name, head->data);
        break;   
    case BMP:
        load_bmp(head->data->name, head->data);
        break;
    case PNG:
        load_png(head->data->name, head->data);
        break;
    }
}